package com.gxa.employment.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.employment.common.Result;
import com.gxa.employment.entity.dto.PageBean;
import com.gxa.employment.entity.pojo.ClazzStudent;
import com.gxa.employment.entity.pojo.EmpDetail;
import com.gxa.employment.entity.pojo.Employment;
import com.gxa.employment.entity.pojo.Enterprise;
import com.gxa.employment.entity.pojo.Job;
import com.gxa.employment.entity.pojo.Student;
import com.gxa.employment.entity.vo.ManagerRateVo;
import com.gxa.employment.entity.vo.PageAndListsVo;
import com.gxa.employment.entity.vo.PageVo;
import com.gxa.employment.mapper.EmpDetailMapper;
import com.gxa.employment.mapper.EmploymentMapper;
import com.gxa.employment.service.EmploymentService;
import com.gxa.employment.service.impl.EmpDetailServiceImpl;
import com.gxa.employment.service.impl.EnterpriseServiceImpl;
import com.gxa.employment.service.impl.JobServiceImpl;
import com.gxa.employment.service.impl.StudentServiceImpl;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 学生就业信息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-03-25
 */
@RestController
@RequestMapping("/employment")
public class EmploymentController {
    private final EmploymentMapper employmentMapper;
    private final EmpDetailServiceImpl empDetailServiceImpl;
    private final EmploymentService employmentService;
    private final StudentServiceImpl studentServiceImpl;
    private final EnterpriseServiceImpl enterpriseServiceImpl;
    private final JobServiceImpl jobServiceImpl;

    public EmploymentController(EmploymentService employmentService, EmploymentMapper employmentMapper, EmpDetailMapper empDetailMapper, EmpDetailServiceImpl empDetailServiceImpl, StudentServiceImpl studentServiceImpl, EnterpriseServiceImpl enterpriseServiceImpl, JobServiceImpl jobServiceImpl) {
        this.employmentService = employmentService;
        this.employmentMapper = employmentMapper;
        this.empDetailServiceImpl = empDetailServiceImpl;
        this.studentServiceImpl = studentServiceImpl;
        this.enterpriseServiceImpl = enterpriseServiceImpl;
        this.jobServiceImpl = jobServiceImpl;
    }

    //   增
    @PostMapping("/add")
    public Result add(@RequestBody Employment employment) {
        employmentService.addEmp(employment);
        return Result.success();
    }

    //查
    @PostMapping("/getByPage")
    public Result getByPage(@RequestBody PageBean dto) {
        //模糊查询
        LambdaQueryWrapper<EmpDetail> lambdaQuery = Wrappers.lambdaQuery(EmpDetail.class);
        lambdaQuery
                .like(StringUtils.isNotEmpty(dto.getName()), EmpDetail::getStuName, dto.getName())
                .orderByDesc(EmpDetail::getEmpId);
        //分页
        IPage<EmpDetail> page = new Page<>(dto.getPage(), dto.getSize());
        //执行
        empDetailServiceImpl.page(page, lambdaQuery);
        return Result.success(new PageVo<>(page.getTotal(), page.getRecords()));
    }

    @GetMapping("/allList")
    public Result allList() {
        List<Student> list1 = studentServiceImpl.list();
        List<Enterprise> list2 = enterpriseServiceImpl.list();
        List<Job> list3 = jobServiceImpl.list();
        //模糊查询
        LambdaQueryWrapper<EmpDetail> lambdaQuery = Wrappers.lambdaQuery(EmpDetail.class);
        lambdaQuery.orderByDesc(EmpDetail::getEmpId);
        //分页
        IPage<EmpDetail> page = new Page<>(1, 10);
        //执行
       empDetailServiceImpl.page(page, lambdaQuery);
        PageVo<EmpDetail> pageVo = new PageVo<>(page.getTotal(), page.getRecords());
        return Result.success(new PageAndListsVo(pageVo,list1,list2,list3));
    }


    /**
     * 获取管理者比率接口
     * 该接口用于获取当前管理者的比率数据
     *
     * @return 返回包含管理者比率数据的Result对象
     */
    @GetMapping("/managerRate")
    public Result<List<ManagerRateVo>> managerRate() {
        return Result.success(employmentMapper.selectManagerRate());
    }

    /**
     * 获取管理员班级信息
     * 根据管理员姓名获取其管理的班级及学生信息
     *
     * @param pageBean
     * @return 返回包含班级学生信息的Result对象
     */
    @PostMapping("/managerClazzInfo")
    public Result<PageVo<ClazzStudent>> managerClazzInfo(@RequestBody PageBean pageBean) {
        pageBean.setPage((pageBean.getPage() - 1) * pageBean.getSize());
        List<ClazzStudent> list = employmentMapper.selectManagerClazzInfo(pageBean);
        Long total = Long.valueOf(employmentMapper.selectManagerClazzInfoTotal(pageBean));
        return Result.success(new PageVo<>(total, list));
    }
}
